home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_6 / chkaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  937 b   |  43 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. main()
  5. {
  6.     char* remote_addr;
  7.  
  8.     printf("Content-type: text/html\n\n");
  9.  
  10.     printf("<HTML>\n");
  11.     printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
  12.     printf("<BODY>\n");
  13.  
  14.     printf("<H1>CGI Script How-to<BR>determine the client machine's name</H1>\n");
  15.  
  16.     remote_addr = getenv("REMOTE_ADDR");
  17.  
  18.     /*
  19.      * Don't assume an environment variable will always be defined and
  20.      * especially in a C program where an undefined string (or NULL address)
  21.      * when used in a string comparison function (strcmp) or print statement
  22.      * may cause the program to abort or have unpredictable results.
  23.      *
  24.      * Check the value returned by the getenv function.
  25.      */
  26.  
  27.     if (remote_addr != NULL)
  28.     {
  29.         printf("Your Internet address is <B>%s</B>\n", remote_addr);
  30.     }
  31.     else
  32.     {
  33.         printf("I don't know your Internet address. Who are you?\n");
  34.     }
  35.  
  36.     printf("</BODY></HTML>\n");
  37.     exit(0);
  38. }
  39.  
  40. /*
  41.  * end of chkaddr.c
  42.  */
  43.